package com.yunqiao.api.utils;

import com.google.gson.Gson;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtil {


	protected static Gson gson = new Gson();

	protected static CloseableHttpClient HTTP_CLIENT = HttpClients.createDefault();

	@SuppressWarnings("unchecked")
	public static Map<String,Object> doPost(String url, Map<String, String> input) throws Exception {

		HttpPost post = new HttpPost(url);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();

		for (Map.Entry<String, String> entry : input.entrySet()) {
			nvps.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
		}
		//转码  封装成请求实体
		HttpEntity reqEntity = new UrlEncodedFormEntity(nvps, Consts.UTF_8);
		post.setEntity(reqEntity);
//		CloseableHttpResponse response = HTTP_CLIENT.execute(post);
		CloseableHttpResponse response = HttpClientPool.getHttpClient().execute(post);
		try {
			if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
				HttpEntity entity = response.getEntity();
				return gson.fromJson(EntityUtils.toString(entity, "UTF-8"),Map.class);
			}
		} finally {
			response.close();
		}
		return null;
	}
	
	
	
	public static Map<String,Object> doPost(String url,BasicNameValuePair... params) throws Exception {
		HttpPost post = new HttpPost(url);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		if (params!=null&&params.length>0) {
			for (int i = 0; i < params.length; i++) {
				nvps.add(params[i]);
			}
			//转码  封装成请求实体
			HttpEntity reqEntity = new UrlEncodedFormEntity(nvps, Consts.UTF_8);
			post.setEntity(reqEntity);
//			try (CloseableHttpResponse response = HTTP_CLIENT.execute(post)){
			try (CloseableHttpResponse response = HttpClientPool.getHttpClient().execute(post)){
				if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
					HttpEntity entity = response.getEntity();
					return gson.fromJson(EntityUtils.toString(entity, "UTF-8"),Map.class);
				}
			} 
		}
		return null;
	}
	

	public static String doPostReturnStr(String url, Map<String, String> input) throws Exception {

		HttpPost post = new HttpPost(url);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();

		for (Map.Entry<String, String> entry : input.entrySet()) {
			nvps.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
		}
		//转码  封装成请求实体
		HttpEntity reqEntity = new UrlEncodedFormEntity(nvps, Consts.UTF_8);

		post.setEntity(reqEntity);
//		CloseableHttpResponse response = HTTP_CLIENT.execute(post);
		CloseableHttpResponse response = HttpClientPool.getHttpClient().execute(post);
		try {
			if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
				HttpEntity entity = response.getEntity();
				return EntityUtils.toString(entity, "UTF-8");
			}
		} finally {
			response.close();
		}
		return null;
	}
	


}
